package com.hq.ims.logincenter.controller;

import com.hq.ims.data.utils.ErrMsgConstant;
import com.hq.ims.data.vo.UserMapstruct;
import com.hq.ims.logincenter.service.LoginCenterBusService;
import com.hq.utils.util.StringUtils;
import com.hq.utils.vo.ReturnVo;
import com.hq.web.annotations.GetJsonValue;
import com.hq.web.utils.WebUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 登录中心的controller
 */
@Api("登录中心")
@RestController
@RequestMapping("/login")
public class LoginController {

    @Resource
    private LoginCenterBusService loginCenterService;

    /**
     * 用户登陆
     */
    @ApiOperation(value = "账号密码登陆", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/accountPwdLogin", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<UserMapstruct.SuccessLoginVo> accountPwdLogin(
            @GetJsonValue(zh = "加密字符串") String encryptAccountAndPasswordStr,
            @GetJsonValue(zh = "客户端ID", required = false) String pushId
    ) {
        UserMapstruct.SuccessLoginVo vo = loginCenterService.accountPwdLogin(encryptAccountAndPasswordStr, pushId);
        return ReturnVo.buildSuccess(vo);
    }

    /**
     * 用户登陆
     */
    @ApiOperation(value = "用户端验证码登陆", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/smsCodeLogin", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<UserMapstruct.SuccessLoginVo> smsCodeLogin(
            @GetJsonValue(zh = "加密字符串") String encryptAccountAndPasswordStr,
            @GetJsonValue(zh = "客户端ID", required = false) String pushId
    ) {
        UserMapstruct.SuccessLoginVo vo = loginCenterService.smsCodeLogin(encryptAccountAndPasswordStr, pushId);
        return ReturnVo.buildSuccess(vo);
    }

    /**
     * 退出登陆
     */
    @ApiOperation(value = "退出登陆", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
    })
    @PostMapping(value = "/loginOut", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ReturnVo<Boolean> loginOut(
    ) {
        String token = WebUtils.getRequestHeadToken();
        StringUtils.verificationEmpty(token, ErrMsgConstant.USER_UN_LOGIN);
        loginCenterService.loginOut(token);
        return ReturnVo.buildSuccess(true);
    }

}
